package com.tx.vuemybatisplusajax.service.serviceimpl;/*
 *
 * @author 陈粒
 * @version 创建时间：2021/8/28 15:49
 */


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.tx.vuemybatisplusajax.mapper.UserMapper;
import com.tx.vuemybatisplusajax.pojo.User;
import com.tx.vuemybatisplusajax.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    //查询所有数据
    @Override
    public List<User> findall() {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(User::getDps);
        return userMapper.selectList(queryWrapper);
    }

    //删除数据
    @Override
    public void deletebyid(Integer id) {
        userMapper.deleteById(id);
    }

    //插入数据
    @Override
    public void insertuser(User user) {
        userMapper.insert(user);
    }

    //更新数据
    @Override
    public void updatebyid(User user) {
        userMapper.updateById(user);
    }

    @Override
    public List<List<User>> groupUsers() {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(User::getStatus, "请假");
        queryWrapper.ne(User::getStatus, "失联");
        queryWrapper.ne(User::getNickName, "");
        queryWrapper.ne(User::getDps, 0);
        List<User> userList = userMapper.selectList(queryWrapper);
        List<User> userList1 = userList.stream().filter(a -> !a.getJobName().equals("圣教军")).collect(Collectors.toList());
        userList1.sort(Comparator.comparing(User::getDps).reversed());

        List<User> userList2 = userList.stream().filter(a -> a.getJobName().equals("圣教军")).collect(Collectors.toList());
        userList2.sort(Comparator.comparing(User::getDps).reversed());

        List<List<User>> groupUserList = new ArrayList<>();

        for (int i = 0; i < 24; i++) {

            List<User> groupUsers = new ArrayList<>();
            if (userList2.size() > 0 && i != 7 && i != 15) {
                groupUsers.add(userList2.get(0));
                userList2.remove(0);
            }

            if (userList1.size() > 0) {
                groupUsers.add(userList1.get(0));
                userList1.remove(0);
            }
            if (userList1.size() > 0) {
                groupUsers.add(userList1.get(0));
                userList1.remove(0);
            }

            if (i != 7 &&  i != 15) {

                if (userList1.size() > 0) {
                    groupUsers.add(userList1.get(0));
                    userList1.remove(0);
                }
                if (groupUsers.size() < 4) {
                    int size = 4 - groupUsers.size();
                    for (int j = 0; j < size; j++) {
                        if (userList1.size() > 0) {
                            groupUsers.add(userList1.get(0));
                            userList1.remove(0);
                        }
                    }
                }
            }



            if (i != 7 & i != 15) {
                if (groupUsers.size() < 4) {
                    int size = 4 - groupUsers.size();
                    for (int j = 0; j < size; j++) {
                        if (userList2.size() > 0) {
                            groupUsers.add(userList2.get(0));
                            userList2.remove(0);
                        }
                    }
                }
            }
            groupUserList.add(groupUsers);
        }

        return groupUserList;


    }


    @Override
    public List<List<User>> groupUsers2() {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(User::getStatus, "请假");
        queryWrapper.ne(User::getStatus, "失联");
        queryWrapper.ne(User::getNickName, "");
        queryWrapper.ne(User::getDps, 0);
        List<User> userList = userMapper.selectList(queryWrapper);
        userList.forEach(a->a.setDps(a.getDps()+(a.getDps()*a.getDamageBonus())/100));
        List<User> userList1 = userList.stream().filter(a -> !a.getJobName().equals("圣教军")).collect(Collectors.toList());
        userList1.sort(Comparator.comparing(User::getDps).reversed());

        List<User> userList2 = userList.stream().filter(a -> a.getJobName().equals("圣教军")).collect(Collectors.toList());
        userList2.sort(Comparator.comparing(User::getDps).reversed());

        List<List<User>> groupUserList = new ArrayList<>();

        for (int i = 0; i < 24; i++) {

            List<User> groupUsers = new ArrayList<>();
            if (userList2.size() > 0 && i != 7 && i != 15) {
                groupUsers.add(userList2.get(0));
                userList2.remove(0);
            }

            if (userList1.size() > 0) {
                groupUsers.add(userList1.get(0));
                userList1.remove(0);
            }
            if (userList1.size() > 0) {
                groupUsers.add(userList1.get(0));
                userList1.remove(0);
            }

            if (i != 7 &&  i != 15 &&  i != 23) {

                if (userList1.size() > 0) {
                    groupUsers.add(userList1.get(0));
                    userList1.remove(0);
                }
                if (groupUsers.size() < 4) {
                    int size = 4 - groupUsers.size();
                    for (int j = 0; j < size; j++) {
                        if (userList1.size() > 0) {
                            groupUsers.add(userList1.get(0));
                            userList1.remove(0);
                        }
                    }
                }
            }



            if (i != 7 && i != 15 &&  i != 23) {
                if (groupUsers.size() < 4) {
                    int size = 4 - groupUsers.size();
                    for (int j = 0; j < size; j++) {
                        if (userList2.size() > 0) {
                            groupUsers.add(userList2.get(0));
                            userList2.remove(0);
                        }
                    }
                }
            }
            groupUserList.add(groupUsers);
        }

        List<User> groupUsers = new ArrayList<>();
        groupUsers.addAll(userList1);
        groupUsers.addAll(userList2);
        groupUserList.add(groupUsers);
        return groupUserList;


    }

    @Override
    public User getUser(String nickName) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getNickName, nickName);
        User user = userMapper.selectOne(queryWrapper);
        if(user!=null)
        {
            return user;
        }

        queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getStatus, "失联");
        queryWrapper.last(" limit 1 ");
        return userMapper.selectOne(queryWrapper);

    }

    @Override
    public  void  setDefaultDamageBonus()
    {
        LambdaUpdateWrapper<User> updateWrapper=new LambdaUpdateWrapper<>();
        updateWrapper.set(User::getDamageBonus,0);
        List<String> jobList=new ArrayList<>();
        jobList.add("死灵");
        jobList.add("武僧");
        jobList.add("野蛮人");
        updateWrapper.in(User::getJobName,jobList);
        userMapper.update(null,updateWrapper);


        updateWrapper=new LambdaUpdateWrapper<>();
        updateWrapper.set(User::getDamageBonus,20);
        jobList=new ArrayList<>();
        jobList.add("法师");
        jobList.add("猎魔");
        jobList.add("圣教军");
        updateWrapper.in(User::getJobName,jobList);
        userMapper.update(null,updateWrapper);
    }
}
